﻿using BitShuva.Common;
using BitShuva.Models;
using Raven.Client;
using Raven.Client.Document;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web;
using System.Web.Mvc;

namespace BitShuva.Controllers
{
    public class HomeController : Controller
    {
        private static IDocumentStore db = RavenDb.Store;

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public void InvitationSignUp(string email, string description)
        {
            using (var session = db.OpenSession())
            {
                session.Store(new SignUpInfo { Email = email, Description = description });
                session.SaveChanges();
            }
        }

        public ActionResult SignUps()
        {
            using (var session = db.OpenSession())
            {
                var signUps = session
                    .Query<SignUpInfo>()
                    .OrderByDescending(a => a.Id)
                    .Take(30);
                var feedItems = from signUp in signUps.ToArray()
                                select new SyndicationItem(
                                    title: signUp.Email,
                                    content: signUp.Description,
                                    itemAlternateLink: new Uri("http://bitshuva.com/" + Uri.EscapeDataString(signUp.Email)));

                var feed = new SyndicationFeed("BitShuva sign ups", "The latest signups for BitShuva Radio", new Uri("http://bitshuva.com"), feedItems) { Language = "en-US" };
                return new RssActionResult { Feed = feed };
            }
        }
    }
}
